        <!DOCTYPE html>
        <html>
        <head>
                <meta charset="utf-8">
        <title>Vector class / box2d_console Library / Dart Documentation</title>
        <link rel="stylesheet" type="text/css"
            href="../styles.css">
        <link href="http://fonts.googleapis.com/css?family=Open+Sans:400,600,700,800" rel="stylesheet" type="text/css">
        <link rel="shortcut icon" href="../favicon.ico">
        
        </head>
        <body data-library="box2d_console" data-type="Vector">
        <div class="page">
        <div class="header">
          <a href="../index.html"><div class="logo"></div></a>
          <a href="../index.html">Dart Documentation</a>
         &rsaquo; <a href="../box2d_console.html">box2d_console</a> &rsaquo; <a href="../box2d_console/Vector.html">Vector</a>        <div id="search-box">
          <input type="search" name="q" id="q" autocomplete="off"
              class="search-input" placeholder="Search API">
        </div>
        
      </div>
      <div class="drop-down" id="drop-down"></div>
      
        <div class="nav">
        
</div>
<div class="content">
        <h2><strong>Vector</strong>
          class
        </h2>
        
<button id="show-inherited" class="show-inherited">Hide inherited</button>
<div class="doc">
<p>A 2-dimensional vector class.</p>
<pre class="source">
class Vector {
  // Each vector is defined as the vector originating from (0,0) to these x and
  // y coordinates.
  num x;
  num y;

  Vector([this.x = 0, this.y = 0]);

  /**
   * Constructs a new vector with the same coordinates as the given vector.
   */
  Vector.copy(Vector other) : x = other.x, y = other.y;

  /**
   * Returns true if given object is a Vector with the same x and y values as
   * this vector. Returns false otherwise.
   */
  bool operator ==(other) =&gt; x == other.x &amp;&amp; y == other.y;

  /**
   * Update the current vector by adding v.
   */
  Vector addLocal(Vector v) {
    x += v.x;
    y += v.y;
    return this;
  }

  /**
   * Subtracts the given vector from this vector.
   */
  Vector subLocal(Vector other) {
    x -= other.x;
    y -= other.y;
    return this;
  }

  Vector setCoords(num xCoord, num yCoord) {
    x = xCoord;
    y = yCoord;
    return this;
  }

  /** Return the cross product of the twe given vectors. */
  static num crossVectors(Vector v1, Vector v2) =&gt; (v1.x * v2.y - v1.y * v2.x);

  /** Return the dot product of the two given vectors. */
  static num dot(Vector one, Vector two) =&gt; (one.x * two.x + one.y * two.y);

  /**
   * Has the effect of swapping the x and y coordinates of the vector,
   * multiplying both by the given number, and then flipping the sign of the new
   * x coordinate. Returns the result through the out parameter.
   */
  static void crossNumAndVectorToOut(num s, Vector a, Vector out) {
    num tempY = s * a.x;
    out.x = -s * a.y;
    out.y = tempY;
  }

  /**
   * Has the effect of swapping the x and y coordinates of the vector,
   * multiplying both by the given number, and then flipping the sign of the new
   * y coordinate. Returns the result through the out parameter.
   */
  static void crossVectorAndNumToOut(Vector a, num s, Vector out) {
    num tempy = -s * a.x;
    out.x = s * a.y;
    out.y = tempy;
  }

  /**
   * Sets this vector to be a copy of the given vector.
   */
  Vector setFrom(Vector v) {
    setCoords(v.x, v.y);
    return this;
  }

  /**
   * Multiplies this vector by the given number.
   */
  Vector mulLocal(num d) {
    x *= d;
    y *= d;
    return this;
  }

  Vector setZero() {
    setCoords(0, 0);
    return this;
  }

  num get length() =&gt; Math.sqrt(this.lengthSquared);

  /**
   * Take the minimum of each coordinate from the two given vectors and store
   * the result in the given out vector.
   */
  static void minToOut(Vector a, Vector b, Vector out) {
    out.x = a.x &lt; b.x ? a.x : b.x;
    out.y = a.y &lt; b.y ? a.y : b.y;
  }

  /**
   * Take the maximum of each coordinate from the two given vectors and store
   * the result in the given out vector.
   */
  static void maxToOut(Vector a, Vector b, Vector out) {
    out.x = a.x &gt; b.x ? a.x : b.x;
    out.y = a.y &gt; b.y ? a.y : b.y;
  }

  /**
   * Returns the length of this vector, squared.
   */
  num get lengthSquared() =&gt; x * x + y * y;

  /**
   * Set the x and y coordinates of this vector to absolute values.
   */
  void absLocal() {
    x = x.abs();
    y = y.abs();
  }

  /**
   * Normalizes this vector and returns the length before normalization.
   */
  num normalize() {
    num len = length;
    if (len &lt; Settings.EPSILON) {
      return 0;
    }

    num invLength = 1.0 / len;
    x *= invLength;
    y *= invLength;
    return len;
  }

  /**
   * Returns the distance between the point defined by this vector and the given
   * vector.
   */
  num distanceBetween(Vector v) {
    num xDelta = this.x - v.x;
    num yDelta = this.y - v.y;
    return Math.sqrt(xDelta * xDelta + yDelta * yDelta);
  }

  /**
   * Returns a vector that is the result of rotating the original vector by
   * angle degrees counter-clockwise.
   */
  Vector rotate(num angle) {
    Matrix22 mtx = new Matrix22();
    mtx.setAngle(angle);
    Vector temp = new Vector();
    mtx.multiplyVectorToOut(this, temp);
    return temp;
  }

  /** Flips this vector such that the +/- of each x and y are reversed. */
  Vector negateLocal() {
    x = -x;
    y = -y;
    return this;
  }

  /** Returns if the values of coordinates within the MAX_INTEGER. */
  bool isValid() =&gt;
      !x.isInfinite() &amp;&amp; !x.isNaN() &amp;&amp; !y.isInfinite() &amp;&amp; !y.isNaN();

  /** Returns a String representation of the given vector. */
  String toString() =&gt; "($x, $y)";
}
</pre>
</div>
<div>
<h3>Constructors</h3>
<div class="method"><h4 id="Vector">
<button class="show-code">Code</button>
new <strong>Vector</strong>([num x = 0, num y = 0]) <a class="anchor-link" href="#Vector"
              title="Permalink to Vector.Vector">#</a></h4>
<div class="doc">
<pre class="source">
Vector([this.x = 0, this.y = 0]);
</pre>
</div>
</div>
<div class="method"><h4 id="Vector.copy">
<button class="show-code">Code</button>
new <strong>Vector.copy</strong>(<a href="../box2d_console/Vector.html">Vector</a> other) <a class="anchor-link" href="#Vector.copy"
              title="Permalink to Vector.Vector.copy">#</a></h4>
<div class="doc">
<p>Constructs a new vector with the same coordinates as the given vector.</p>
<pre class="source">
Vector.copy(Vector other) : x = other.x, y = other.y;
</pre>
</div>
</div>
</div>
<div>
<h3>Static Methods</h3>
<div class="method"><h4 id="crossVectors">
<button class="show-code">Code</button>
num <strong>crossVectors</strong>(<a href="../box2d_console/Vector.html">Vector</a> v1, <a href="../box2d_console/Vector.html">Vector</a> v2) <a class="anchor-link" href="#crossVectors"
              title="Permalink to Vector.crossVectors">#</a></h4>
<div class="doc">
<p>Return the cross product of the twe given vectors.</p>
<pre class="source">
static num crossVectors(Vector v1, Vector v2) =&gt; (v1.x * v2.y - v1.y * v2.x);
</pre>
</div>
</div>
<div class="method"><h4 id="dot">
<button class="show-code">Code</button>
num <strong>dot</strong>(<a href="../box2d_console/Vector.html">Vector</a> one, <a href="../box2d_console/Vector.html">Vector</a> two) <a class="anchor-link" href="#dot"
              title="Permalink to Vector.dot">#</a></h4>
<div class="doc">
<p>Return the dot product of the two given vectors.</p>
<pre class="source">
static num dot(Vector one, Vector two) =&gt; (one.x * two.x + one.y * two.y);
</pre>
</div>
</div>
<div class="method"><h4 id="crossNumAndVectorToOut">
<button class="show-code">Code</button>
void <strong>crossNumAndVectorToOut</strong>(num s, <a href="../box2d_console/Vector.html">Vector</a> a, <a href="../box2d_console/Vector.html">Vector</a> out) <a class="anchor-link" href="#crossNumAndVectorToOut"
              title="Permalink to Vector.crossNumAndVectorToOut">#</a></h4>
<div class="doc">
<p>Has the effect of swapping the x and y coordinates of the vector,
multiplying both by the given number, and then flipping the sign of the new
x coordinate. Returns the result through the out parameter.</p>
<pre class="source">
static void crossNumAndVectorToOut(num s, Vector a, Vector out) {
  num tempY = s * a.x;
  out.x = -s * a.y;
  out.y = tempY;
}
</pre>
</div>
</div>
<div class="method"><h4 id="crossVectorAndNumToOut">
<button class="show-code">Code</button>
void <strong>crossVectorAndNumToOut</strong>(<a href="../box2d_console/Vector.html">Vector</a> a, num s, <a href="../box2d_console/Vector.html">Vector</a> out) <a class="anchor-link" href="#crossVectorAndNumToOut"
              title="Permalink to Vector.crossVectorAndNumToOut">#</a></h4>
<div class="doc">
<p>Has the effect of swapping the x and y coordinates of the vector,
multiplying both by the given number, and then flipping the sign of the new
y coordinate. Returns the result through the out parameter.</p>
<pre class="source">
static void crossVectorAndNumToOut(Vector a, num s, Vector out) {
  num tempy = -s * a.x;
  out.x = s * a.y;
  out.y = tempy;
}
</pre>
</div>
</div>
<div class="method"><h4 id="minToOut">
<button class="show-code">Code</button>
void <strong>minToOut</strong>(<a href="../box2d_console/Vector.html">Vector</a> a, <a href="../box2d_console/Vector.html">Vector</a> b, <a href="../box2d_console/Vector.html">Vector</a> out) <a class="anchor-link" href="#minToOut"
              title="Permalink to Vector.minToOut">#</a></h4>
<div class="doc">
<p>Take the minimum of each coordinate from the two given vectors and store
the result in the given out vector.</p>
<pre class="source">
static void minToOut(Vector a, Vector b, Vector out) {
  out.x = a.x &lt; b.x ? a.x : b.x;
  out.y = a.y &lt; b.y ? a.y : b.y;
}
</pre>
</div>
</div>
<div class="method"><h4 id="maxToOut">
<button class="show-code">Code</button>
void <strong>maxToOut</strong>(<a href="../box2d_console/Vector.html">Vector</a> a, <a href="../box2d_console/Vector.html">Vector</a> b, <a href="../box2d_console/Vector.html">Vector</a> out) <a class="anchor-link" href="#maxToOut"
              title="Permalink to Vector.maxToOut">#</a></h4>
<div class="doc">
<p>Take the maximum of each coordinate from the two given vectors and store
the result in the given out vector.</p>
<pre class="source">
static void maxToOut(Vector a, Vector b, Vector out) {
  out.x = a.x &gt; b.x ? a.x : b.x;
  out.y = a.y &gt; b.y ? a.y : b.y;
}
</pre>
</div>
</div>
</div>
<div>
<h3>Properties</h3>
<div class="field"><h4 id="length">
<button class="show-code">Code</button>
final num         <strong>length</strong> <a class="anchor-link"
            href="#length"
            title="Permalink to Vector.length">#</a>
        </h4>
        <div class="doc">
<pre class="source">
num get length() =&gt; Math.sqrt(this.lengthSquared);
</pre>
</div>
</div>
<div class="field"><h4 id="lengthSquared">
<button class="show-code">Code</button>
final num         <strong>lengthSquared</strong> <a class="anchor-link"
            href="#lengthSquared"
            title="Permalink to Vector.lengthSquared">#</a>
        </h4>
        <div class="doc">
<p>Returns the length of this vector, squared.</p>
<pre class="source">
num get lengthSquared() =&gt; x * x + y * y;
</pre>
</div>
</div>
<div class="field inherited"><h4 id="runtimeType">
<button class="show-code">Code</button>
final Type         <strong>runtimeType</strong> <a class="anchor-link"
            href="#runtimeType"
            title="Permalink to Vector.runtimeType">#</a>
        </h4>
        <div class="inherited-from">inherited from Object </div><div class="doc">
<p>A representation of the runtime type of the object.</p>
<pre class="source">
external Type get runtimeType;
</pre>
</div>
</div>
<div class="field"><h4 id="x">
<button class="show-code">Code</button>
num         <strong>x</strong> <a class="anchor-link"
            href="#x"
            title="Permalink to Vector.x">#</a>
        </h4>
        <div class="doc">
<pre class="source">
num x;
</pre>
</div>
</div>
<div class="field"><h4 id="y">
<button class="show-code">Code</button>
num         <strong>y</strong> <a class="anchor-link"
            href="#y"
            title="Permalink to Vector.y">#</a>
        </h4>
        <div class="doc">
<pre class="source">
num y;
</pre>
</div>
</div>
</div>
<div>
<h3>Operators</h3>
<div class="method"><h4 id="==">
<button class="show-code">Code</button>
bool <strong>operator ==</strong>(other) <a class="anchor-link" href="#=="
              title="Permalink to Vector.operator ==">#</a></h4>
<div class="doc">
<p>Returns true if given object is a Vector with the same x and y values as
this vector. Returns false otherwise.</p>
<pre class="source">
bool operator ==(other) =&gt; x == other.x &amp;&amp; y == other.y;
</pre>
</div>
</div>
</div>
<div>
<h3>Methods</h3>
<div class="method"><h4 id="absLocal">
<button class="show-code">Code</button>
void <strong>absLocal</strong>() <a class="anchor-link" href="#absLocal"
              title="Permalink to Vector.absLocal">#</a></h4>
<div class="doc">
<p>Set the x and y coordinates of this vector to absolute values.</p>
<pre class="source">
void absLocal() {
  x = x.abs();
  y = y.abs();
}
</pre>
</div>
</div>
<div class="method"><h4 id="addLocal">
<button class="show-code">Code</button>
<a href="../box2d_console/Vector.html">Vector</a> <strong>addLocal</strong>(<a href="../box2d_console/Vector.html">Vector</a> v) <a class="anchor-link" href="#addLocal"
              title="Permalink to Vector.addLocal">#</a></h4>
<div class="doc">
<p>Update the current vector by adding v.</p>
<pre class="source">
Vector addLocal(Vector v) {
  x += v.x;
  y += v.y;
  return this;
}
</pre>
</div>
</div>
<div class="method"><h4 id="distanceBetween">
<button class="show-code">Code</button>
num <strong>distanceBetween</strong>(<a href="../box2d_console/Vector.html">Vector</a> v) <a class="anchor-link" href="#distanceBetween"
              title="Permalink to Vector.distanceBetween">#</a></h4>
<div class="doc">
<p>Returns the distance between the point defined by this vector and the given
vector.</p>
<pre class="source">
num distanceBetween(Vector v) {
  num xDelta = this.x - v.x;
  num yDelta = this.y - v.y;
  return Math.sqrt(xDelta * xDelta + yDelta * yDelta);
}
</pre>
</div>
</div>
<div class="method inherited"><h4 id="hashCode">
<button class="show-code">Code</button>
int <strong>hashCode</strong>() <a class="anchor-link" href="#hashCode"
              title="Permalink to Vector.hashCode">#</a></h4>
<div class="inherited-from">inherited from Object </div><div class="doc">
<p>Get a hash code for this object.</p>
<p>All objects have hash codes. Hash codes are guaranteed to be the
same for objects that are equal when compared using the equality
operator <code>==</code>. Other than that there are no guarantees about
the hash codes. They will not be consistent between runs and
there are no distribution guarantees.</p>
<p>If a subclass overrides <code>hashCode</code> it should override the
equality operator as well to maintain consistency.</p>
<pre class="source">
external int hashCode();
</pre>
</div>
</div>
<div class="method"><h4 id="isValid">
<button class="show-code">Code</button>
bool <strong>isValid</strong>() <a class="anchor-link" href="#isValid"
              title="Permalink to Vector.isValid">#</a></h4>
<div class="doc">
<p>Returns if the values of coordinates within the MAX_INTEGER.</p>
<pre class="source">
bool isValid() =&gt;
    !x.isInfinite() &amp;&amp; !x.isNaN() &amp;&amp; !y.isInfinite() &amp;&amp; !y.isNaN();
</pre>
</div>
</div>
<div class="method"><h4 id="mulLocal">
<button class="show-code">Code</button>
<a href="../box2d_console/Vector.html">Vector</a> <strong>mulLocal</strong>(num d) <a class="anchor-link" href="#mulLocal"
              title="Permalink to Vector.mulLocal">#</a></h4>
<div class="doc">
<p>Multiplies this vector by the given number.</p>
<pre class="source">
Vector mulLocal(num d) {
  x *= d;
  y *= d;
  return this;
}
</pre>
</div>
</div>
<div class="method"><h4 id="negateLocal">
<button class="show-code">Code</button>
<a href="../box2d_console/Vector.html">Vector</a> <strong>negateLocal</strong>() <a class="anchor-link" href="#negateLocal"
              title="Permalink to Vector.negateLocal">#</a></h4>
<div class="doc">
<p>Flips this vector such that the +/- of each x and y are reversed.</p>
<pre class="source">
Vector negateLocal() {
  x = -x;
  y = -y;
  return this;
}
</pre>
</div>
</div>
<div class="method"><h4 id="normalize">
<button class="show-code">Code</button>
num <strong>normalize</strong>() <a class="anchor-link" href="#normalize"
              title="Permalink to Vector.normalize">#</a></h4>
<div class="doc">
<p>Normalizes this vector and returns the length before normalization.</p>
<pre class="source">
num normalize() {
  num len = length;
  if (len &lt; Settings.EPSILON) {
    return 0;
  }

  num invLength = 1.0 / len;
  x *= invLength;
  y *= invLength;
  return len;
}
</pre>
</div>
</div>
<div class="method inherited"><h4 id="noSuchMethod">
<button class="show-code">Code</button>
<strong>noSuchMethod</strong>(String name, List args) <a class="anchor-link" href="#noSuchMethod"
              title="Permalink to Vector.noSuchMethod">#</a></h4>
<div class="inherited-from">inherited from Object </div><div class="doc">
<p><code>noSuchMethod</code> is invoked when users invoke a non-existant method
on an object. The name of the method and the arguments of the
invocation are passed to <code>noSuchMethod</code>. If <code>noSuchMethod</code>
returns a value, that value becomes the result of the original
invocation.</p>
<p>The default behavior of <code>noSuchMethod</code> is to throw a
<code>noSuchMethodError</code>.</p>
<pre class="source">
external Dynamic noSuchMethod(String name, List args);
</pre>
</div>
</div>
<div class="method inherited"><h4 id="Object">
<button class="show-code">Code</button>
const <strong>Object</strong>() <a class="anchor-link" href="#Object"
              title="Permalink to Vector.Object">#</a></h4>
<div class="inherited-from">inherited from Object </div><div class="doc">
<p>Creates a new <code>Object</code> instance.</p>
<p><code>Object</code> instances have no meaningful state, and are only useful
through their identity. An <code>Object</code> instance is equal to itself
only.</p>
<pre class="source">
const Object();
</pre>
</div>
</div>
<div class="method"><h4 id="rotate">
<button class="show-code">Code</button>
<a href="../box2d_console/Vector.html">Vector</a> <strong>rotate</strong>(num angle) <a class="anchor-link" href="#rotate"
              title="Permalink to Vector.rotate">#</a></h4>
<div class="doc">
<p>Returns a vector that is the result of rotating the original vector by
angle degrees counter-clockwise.</p>
<pre class="source">
Vector rotate(num angle) {
  Matrix22 mtx = new Matrix22();
  mtx.setAngle(angle);
  Vector temp = new Vector();
  mtx.multiplyVectorToOut(this, temp);
  return temp;
}
</pre>
</div>
</div>
<div class="method"><h4 id="setCoords">
<button class="show-code">Code</button>
<a href="../box2d_console/Vector.html">Vector</a> <strong>setCoords</strong>(num xCoord, num yCoord) <a class="anchor-link" href="#setCoords"
              title="Permalink to Vector.setCoords">#</a></h4>
<div class="doc">
<pre class="source">
Vector setCoords(num xCoord, num yCoord) {
  x = xCoord;
  y = yCoord;
  return this;
}
</pre>
</div>
</div>
<div class="method"><h4 id="setFrom">
<button class="show-code">Code</button>
<a href="../box2d_console/Vector.html">Vector</a> <strong>setFrom</strong>(<a href="../box2d_console/Vector.html">Vector</a> v) <a class="anchor-link" href="#setFrom"
              title="Permalink to Vector.setFrom">#</a></h4>
<div class="doc">
<p>Sets this vector to be a copy of the given vector.</p>
<pre class="source">
Vector setFrom(Vector v) {
  setCoords(v.x, v.y);
  return this;
}
</pre>
</div>
</div>
<div class="method"><h4 id="setZero">
<button class="show-code">Code</button>
<a href="../box2d_console/Vector.html">Vector</a> <strong>setZero</strong>() <a class="anchor-link" href="#setZero"
              title="Permalink to Vector.setZero">#</a></h4>
<div class="doc">
<pre class="source">
Vector setZero() {
  setCoords(0, 0);
  return this;
}
</pre>
</div>
</div>
<div class="method"><h4 id="subLocal">
<button class="show-code">Code</button>
<a href="../box2d_console/Vector.html">Vector</a> <strong>subLocal</strong>(<a href="../box2d_console/Vector.html">Vector</a> other) <a class="anchor-link" href="#subLocal"
              title="Permalink to Vector.subLocal">#</a></h4>
<div class="doc">
<p>Subtracts the given vector from this vector.</p>
<pre class="source">
Vector subLocal(Vector other) {
  x -= other.x;
  y -= other.y;
  return this;
}
</pre>
</div>
</div>
<div class="method"><h4 id="toString">
<button class="show-code">Code</button>
String <strong>toString</strong>() <a class="anchor-link" href="#toString"
              title="Permalink to Vector.toString">#</a></h4>
<div class="doc">
<p>Returns a String representation of the given vector.</p>
<pre class="source">
String toString() =&gt; "($x, $y)";
</pre>
</div>
</div>
<div class="method"><h4 id="Vector">
<button class="show-code">Code</button>
new <strong>Vector</strong>([num x = 0, num y = 0]) <a class="anchor-link" href="#Vector"
              title="Permalink to Vector.Vector">#</a></h4>
<div class="doc">
<pre class="source">
Vector([this.x = 0, this.y = 0]);
</pre>
</div>
</div>
<div class="method"><h4 id="Vector.copy">
<button class="show-code">Code</button>
new <strong>Vector.copy</strong>(<a href="../box2d_console/Vector.html">Vector</a> other) <a class="anchor-link" href="#Vector.copy"
              title="Permalink to Vector.Vector.copy">#</a></h4>
<div class="doc">
<p>Constructs a new vector with the same coordinates as the given vector.</p>
<pre class="source">
Vector.copy(Vector other) : x = other.x, y = other.y;
</pre>
</div>
</div>
</div>
        </div>
        <div class="clear"></div>
        </div>
        
        <div class="footer">
          
        </div>
        <script async src="../client-live-nav.js"></script>
        </body></html>
        
